var page = sm("do_Page");
var app = sm("do_App");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var root = ui("$");
var http = require("helper/http");
var mallUrl = require("url/mall");
var date = sm("do_DateTimePicker");
var startTime = ui("startTime");
var endTime = ui("endTime");

var shopId = null;
var busStart = null;
var busEnd = null;
//获取上一个页面传入的参数
page.on("loaded", function(){
	var params = page.getData();
	shopId = params.id;
	busStart = params.busStart;
	busEnd = params.busEnd;
	startTime.text = busStart;
	endTime.text = busEnd;
});

//返回键
page.on("back", function() {
    app.closePage();
});

var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
var header = ui("header");
ui(addheader + ".labelTitle").text = "修改营业时间";
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
    app.closePage();
});

//保存
var headerRight = ui(addheader+".headerRight");
var headerRight_lableNext = ui(addheader+".labelNext");
headerRight_lableNext.text = "保存";
headerRight_lableNext.visible = true;
headerRight.on("touch", function() {
	submit();
});

function submit(){
	var paramData = {
		id:shopId,
		busStart:busStart,
		busEnd:busEnd
	};
	http.post(mallUrl.url.shop.updateBusTime, paramData, function(data) {
		app.fire("fireShopUpdate",{"busStart":busStart,"busEnd":busEnd});
		app.closePage();
	});
};

var linearLayout_main = ui("do_LinearLayout_main");

//定义时间
var myDate=new Date();
var date0 = myDate.getTime();
//定义最小时间
myDate.setHours(0,0,0,0);
var dateMin = myDate.getTime();
//定义最大时间
myDate.setHours(23,59,59,999);
var dateMax = myDate.getTime();
//选择时间方式，最大最小日期为默认，预设时间为当前时间，不显示按钮
ui("startTimeBox").on("touch","",300,function(){
    date.show({type:2, data:date0, maxDate:dateMax,minDate:dateMin, title:"时间选择", buttons:["选择","不选择"]}, function(data, e){
    	if (data.flag == 0) {
    		var sTime=parseInt(data.time,10);
        	startTime.text = timeFormat(sTime);
        	busStart = startTime.text;
    	}
    });
})

ui("endTimeBox").on("touch","",300,function(){
    date.show({type:2, data:date0, maxDate:dateMax,minDate:dateMin, title:"时间选择", buttons:["选择","不选择"]}, function(data, e){
    	if (data.flag == 0) {
    		var eTime=parseInt(data.time,10);
        	endTime.text = timeFormat(eTime);
        	busEnd = endTime.text;
    	}
    });
})

function timeFormat(time){
	var svDate = new Date(time);//要格式化的时间
	var minutes=0;
	if(svDate.getMinutes()<10){
		minutes="0"+svDate.getMinutes();
	}else{
		minutes=svDate.getMinutes();
	}
	var hours=0;
	if(svDate.getHours()<10){
		hours="0"+svDate.getHours();
	}else{
		hours=svDate.getHours();
	}
	return hours+':'+minutes;
}